/*
* © 2010 Autodata Solutions Company. All Rights Reserved
* This source code is the confidential and proprietary information of Autodata Solutions Company.
* The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code
* without the prior written consent of Autodata Solutions Company.
*/
Ext.ns('net.autodata.compare');
(function () {
	
	var Proxy = Ext.extend(Ext.data.DataProxy, {
		
		constructor: function ()
		{
			var api = {};
			api[Ext.data.Api.actions.read] = true;
			Proxy.superclass.constructor.call(this, {api: api});
		},
		
		doRequest: function (action, rs, params, reader, callback, scope, arg)
		{
			// No implementation for CRUD in MemoryProxy.  Assumes all actions are 'load'
			
			//Grab the base vehicle and competitors from Session
			
			var session = net.autodata.Session.getInstance();
			var vehicles = session.vehicles;
			
			var data = {
				rows: vehicles.getCommonCompetitors(false)
			};
						
			try {
				result = reader.readRecords(data);
			} catch(e) {
				// @deprecated loadexception
				this.fireEvent("loadexception", this, null, arg, e);
	
				this.fireEvent('exception', this, 'response', action, arg, null, e);
				callback.call(scope, null, arg, false);
				return;
			}
			callback.call(scope, result, arg, true);
		}		
	});
	
	var Store = Ext.extend(Ext.data.Store, {
		constructor: function ()
		{
			//Private helper functions
			function convertCode(o)
			{
				return o.code;
			}
			
			function convertDesc(o)
			{
				return o.description;
			}
									
			Store.superclass.constructor.call(this, {
				autoLoad: false,
				reader: new Ext.data.JsonReader({
					idProperty: 'acode',
					root: 'rows',
					
					fields: [
						{name: 'acode'},
						{name: 'modelYearCode', mapping: 'modelYear', convert: convertCode},
						{name: 'modelYear', mapping: 'modelYear', convert: convertDesc},
						{name: 'makeCode', mapping: 'make', convert: convertCode},
						{name: 'make', mapping: 'make', convert: convertDesc},
						{name: 'modelCode', mapping: 'model', convert: convertCode},
						{name: 'model', mapping: 'model', convert: convertDesc},
						{name: 'trimCode', mapping: 'trim', convert: convertCode},
						{name: 'trim', mapping: 'trim', convert: convertDesc},
						{name: 'imageName'},
						{name: 'msrp', type: 'float'}
					]
				}),
				
				proxy: new Proxy()
			});
		}
	});
	
	
	/**
	 * Customized Panel to display a list of Common Competitors
	 * @class CommonCompetitorsPanel
	 * @extends Ext.BoxComponent
	 * @namespace net.autodata.compare
	 */
	var Panel = Ext.extend(Ext.BoxComponent, {
		
		autoEl: {
			tag: 'div',
			cls: 'commonCompetitors-panel'
		},
		
		/**
		 * Object containing view configuration values
		 * @property viewCfg
		 * @type Object
		 */
		viewCfg: undefined,
		
		/**
		 * View to be used when rendering this panel
		 * @property view
		 * @type Object
		 */
		view: undefined,
		
		constructor: function ()
		{			
			this.addEvents({'select': true});
			this.store = new Store();
			Panel.superclass.constructor.apply(this, arguments);
		},
		
		initComponent: function ()
		{
			Panel.superclass.initComponent.apply(this, arguments);
			
			var view = this.getView();
			view.init(this);
		},
		
		/**
		 * @method getView
		 */
		getView: function ()
		{
			if (!this.view) {
				this.view = new net.autodata.compare.CommonCompetitorsView();
			}
			
			return this.view;
		},
		
		/**
		 * @method getStore
		 */
		getStore: function ()
		{
			return this.store;
		},
		
		/**
		 * @method onRender
		 * @private
		 */
		onRender: function()
		{	
			Panel.superclass.onRender.apply(this, arguments);
			
			var body = this.el;
			Ext.fly(body.dom).on('click', this.onClick, this, {delegate: 'li'});			
		},
		
		/**
		 * @method onClick
		 * @private
		 */
		onClick: function (e, t)
		{
			var acode = Ext.fly(t).getAttribute('acode');
			if (Ext.isEmpty(acode)) {
				return;
			}
			
			var record = this.getStore().getById(acode);
			if (!record) {
				return;
			}
			
			this.fireEvent('select', t, record);
		}		
	});	
	
	net.autodata.compare.CommonCompetitorsPanel = Panel;
	
})();